package com.blinddate.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.blinddate.orm.BdCountry;

public class CountryDAO extends HibernateDaoSupport implements com.blinddate.dao.CountryDAO {

	@Override
	public boolean addCountry(String countryName) {
		BdCountry country = new BdCountry(countryName);
		return this.addCountry(country);
	}

	@Override
	public boolean addCountry(BdCountry country) {
		boolean addSuccess = false;
		try{
			this.getHibernateTemplate().save(country);
			addSuccess = true;
		}catch(Exception e){
			e.printStackTrace();
			addSuccess = false;
		}
		return addSuccess;
	}

	@Override
	public boolean deleteCountry(String countryName) {
		BdCountry country = new BdCountry(countryName);
		return this.deleteCountry(country);
	}

	@Override
	public boolean deleteCountry(BdCountry country) {
		boolean deleteSuccess = false;
		try{
			this.getHibernateTemplate().delete(country);
			deleteSuccess = true;
		}catch(Exception e){
			e.printStackTrace();
			deleteSuccess = false;
		}
		return deleteSuccess;
	}

	@Override
	public boolean updateCountry(BdCountry country) {
		boolean updateSuccess = false;
		try{
			updateSuccess = true;
			this.getHibernateTemplate().update(country);
		}catch(Exception e){
			updateSuccess = false;
			e.printStackTrace();	
		}
		return updateSuccess;
	}

	@Override
	public List<BdCountry> getAllCountries(){
		List<BdCountry> countries = null;
		try{
			String queryCondition = "from BdCountry";
			countries = (List<BdCountry>)this.getHibernateTemplate().find(queryCondition);
		}catch(Exception e){
			countries = null;
			e.printStackTrace();
		} 
		return countries;
	}
	
	@Override
	public BdCountry getCountry(String countryName){
		BdCountry country = null;
		try{
			String queryCondition = "from BdCountry where name = ?";
			country = (BdCountry)this.getHibernateTemplate().find(queryCondition, countryName);
		}catch(Exception e){
			country = null;
			e.printStackTrace();
		}
		return country;
	}
	
	@Override
	public BdCountry getCountry(Integer id){
		BdCountry country = null;
		try{
			String queryCondition = "from BdCountry where id = ?";
			country = (BdCountry)this.getHibernateTemplate().find(queryCondition, id);
		}catch(Exception e){
			country = null;
			e.printStackTrace();
		}
		return country;
	}
}
